import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Store from '../store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home/actor-list',
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    meta: {
      breadcrumbs: ['首页'],
    },
    children: [
      // {
      //   path: 'index',
      //   name: '/home/index',
      //   component: () => import('../views/Index.vue'),
      // },
      {
        path: 'index',
        name: '/home/actor-list',
        component: () => import('../views/actor/ActorList.vue'),
        //meta称为路由对象的元数据,在此可以自定义配置一些路由相关参数
        //方便在组件中使用
        meta: {
          breadcrumbs: ['首页', '演员管理', '演员列表'],
        },
      },
      {
        path: 'actor-add',
        name: '/home/actor-add',
        component: () => import('../views/actor/ActorAdd.vue'),
        meta: {
          breadcrumbs: ['首页', '演员管理', '添加演员'],
        },
      },
      {
        path: 'director-list',
        name: '/home/director-list',
        component: () => import('../views/director/DirectorList.vue'),
        meta: {
          breadcrumbs: ['首页', '导演管理', '导演列表'],
        },
      },
      {
        path: 'director-add',
        name: '/home/director-add',
        component: () => import('../views/director/DirectorAdd.vue'),
        meta: {
          breadcrumbs: ['首页', '导演管理', '添加导演'],
        },
      },
      {
        path: 'movie-list',
        name: '/home/movie-list',
        component: () => import('../views/movie/MovieList.vue'),
        meta: {
          breadcrumbs: ['首页', '电影管理', '电影列表'],
        },
      },
      {
        path: 'movie-add',
        name: '/home/movie-add',
        component: () => import('../views/movie/MovieAdd.vue'),
        meta: {
          breadcrumbs: ['首页', '电影管理', '添加电影'],
        },
      },
      {
        path: 'movie-update/:id',
        name: '/home/movie-update',
        component: () => import('../views/movie/MovieUpdate.vue'),
        meta: {
          breadcrumbs: ['首页', '电影管理', '电影列表', '修改电影'],
        },
      },
      {
        path: 'cinema-list',
        name: '/home/cinema-list',
        component: () => import('../views/cinema/CinemaLis.vue'),
        meta: {
          breadcrumbs: ['首页', '电影院管理', '电影院列表'],
        },
      },
      {
        path: 'cinema-add',
        name: '/home/cinema-add',
        component: () => import('../views/cinema/CinemaAdd.vue'),
        meta: {
          breadcrumbs: ['首页', '电影院管理', '添加电影院'],
        },
      },
      {
        path: 'cinema-room-list/:cinemaId',
        name: '/home/cinema-room-list',
        component: () => import('../views/cinema/CinemaRoomList.vue'),
        meta: {
          breadcrumbs: ['首页', '电影院管理', '电影院列表', '放映厅列表'],
        },
      },
      {
        path: 'showingon-plan-add/:roomId',
        name: '/home/showingon-plan-add',
        component: () => import('../views/cinema/ShowingonplanAdd.vue'),
        meta: {
          breadcrumbs: [
            '首页',
            '电影院管理',
            '电影院列表',
            '放映厅列表',
            '添加排片计划',
          ],
        },
      },
      {
        path: 'showingon-plan-list/:roomId',
        name: '/home/showingon-plan-list',
        component: () => import('../views/cinema/ShowingonplanList.vue'),
        meta: {
          breadcrumbs: [
            '首页',
            '电影院管理',
            '电影院列表',
            '放映厅列表',
            '排片计划列表',
          ],
        },
      },
      {
        path: 'seat-template/:id',
        name: '/home/seat-template',
        component: () => import('../views/cinema/CinemaRoomSeatTemplate.vue'),
        meta: {
          breadcrumbs: [
            '首页',
            '电影院管理',
            '电影院列表',
            '放映厅列表',
            '配置作为模板',
          ],
        },
      },
    ],
  },

  {
    path: '/user/login',
    name: '/user/login',
    component: () => import('../views/user/Login.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

// 注册全局的前置路由守卫，监控路由的跳转过程：当从A跳转到B之前执行
router.beforeEach((to, from, next) => {
  // console.log(from) // 起点路由对象
  // console.log(to)   // 目标路由对象
  // 判断当前是否跳转到登录页
  if (to.path == '/user/login') {
    // 不拦截去往登录页面
    next()
  } else {
    // 判断用户是否已经登录，只有登录过的用户，才可以跳转到其他页面
    if (Store.state.user) {
      next()
    } else {
      // 否则，未登录直接跳转到登录页面
      next('/user/login')
    }
  }

  // next('/') // 或者 next({ path: '/' }): 跳转到一个不同的地址。
  // next()   // 方法，调用此方法，才可以执行正常的后续流程
})

export default router
